﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Sita.Entities;

namespace Sita.Presentation.Models
{
    [Serializable]
    public class UserSession
    {
        public UserSession(User data, IList<Permission> permissions, DateTime loginTime, BusinessUnit bu, TimeSpan hoursDifference, IList<Module> modules)
        {
            List<BusinessUnit> bus = new List<BusinessUnit>();
            bus.Add(bu);

            if (data != null)
            {
                Data = Code.SessionModelManager.GetSessionUser(data);
            }
            if (bus != null)
            {
                Business = Code.SessionModelManager.GetSessionBusinessUnits(bus);
            }
            if (permissions != null)
            {
                Permissions = Code.SessionModelManager.GetSessionPermissions(permissions);
            }

            //if (groups != null)
            //{
            //    Groups = Code.SessionModelManager.GetSessionGroups(groups);
            //}
            LoginTime = loginTime;
            CurrentBusinessUnitIndex = 0;
            CurrentBusinessID = -1;

            //if (groups != null)
            //{
            //    for (int i = 0; i < groups.Count; i++)
            //    {
            //        for (int j = 0; j < groups[i].Modules.Count; j++)
            //        {
            //            if (groups[i].Modules[j].ModuleBusinesses.Count > 0)
            //            {
            //                CurrentBusinessID = groups[i].Modules[j].ModuleBusinesses[0].BusinessID;
            //                break;
            //            }
            //        }
            //        if (CurrentBusinessID > -1)
            //        {
            //            break;
            //        }
            //    }
            //}

            if (hoursDifference != null)
            {
                HoursDifference = hoursDifference;
            }

            Modules = modules;

            Options = new Dictionary<string, object>();
            Options.Add("PageSize", 10);
        }

        public string CurrentModuleCode { get; set; }
        public int CurrentBusinessID { get; set; }
        public UserSessionUser Data { get; private set; }
        //public IList<UserSessionGroup> Groups { get; private set; }
        public DateTime LoginTime { get; private set; }
        public Dictionary<string, object> Options { get; private set; }
        public IList<UserSessionBusinessUnit> Business { get; set; }
        public int CurrentBusinessUnitIndex { get; set; }
        protected IList<UserSessionPermission> Permissions { get; private set; }
        public TimeSpan HoursDifference { get; private set; }
        public IList<Module> Modules { get; set; }

        public bool CheckPermissionAvailavility(string permissionCode)
        {
            return Permissions.Where(p => p.Code == permissionCode).Count() > 0;
        }
    }
}